﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.InvMovementServiceRef;

    public class OrderMovementDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected DropDownList ddlStatus;
        protected HtmlForm F5223;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnFromLoc;
        protected HtmlInputHidden hdnFromLpn;
        protected HtmlInputHidden hdnLot;
        protected HtmlInputHidden hdnMovementID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected HtmlImage img1;
        protected HtmlImage img2;
        protected HtmlImage img3;
        protected HtmlImage imgOwnerID;
        protected Label lblAvailableQty;
        protected Label lblExpiryDate;
        protected Label lblExternalLot;
        protected Label lblFromLoc;
        protected Label lblFromLpnID;
        protected Label lblFromQty;
        protected Label lblLineID;
        protected Label lblLot;
        protected Label lblLotAttr01;
        protected Label lblLotAttr02;
        protected Label lblLotAttr03;
        protected Label lblLotAttr04;
        protected Label lblLotAttr05;
        protected Label lblLotAttr06;
        protected Label lblLotAttr07;
        protected Label lblLotAttr09;
        protected Label lblLotAttr10;
        protected Label lblMsg;
        protected Label lblOrderMovementDetail;
        protected Label lblOwnerID;
        protected Label lblPackID;
        protected Label lblProduceDate;
        protected Label lblQty;
        protected Label lblReceiptDate;
        protected Label lblRemark;
        protected Label lblSkuDescr;
        protected Label lblSkuID;
        protected Label lblStatus;
        protected Label lblToLoc;
        protected Label lblToLpnID;
        protected Label lblUomID;
        protected Label lblVendorID;
        protected Label LotAttr08;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAvailableQty;
        protected TextBox txtExpiryDate;
        protected TextBox txtExternalLot;
        protected TextBox txtFromLoc;
        protected TextBox txtFromLpnID;
        protected TextBox txtFromQty;
        protected TextBox txtLineID;
        protected TextBox txtLot;
        protected TextBox txtLotAttr01;
        protected TextBox txtLotAttr02;
        protected TextBox txtLotAttr03;
        protected TextBox txtLotAttr04;
        protected TextBox txtLotAttr05;
        protected TextBox txtLotAttr06;
        protected TextBox txtLotAttr07;
        protected TextBox txtLotAttr08;
        protected TextBox txtLotAttr09;
        protected TextBox txtLotAttr10;
        protected TextBox txtOwnerID;
        protected TextBox txtPackID;
        protected TextBox txtProduceDate;
        protected TextBox txtQty;
        protected TextBox txtReceiptDate;
        protected TextBox txtRemark;
        protected TextBox txtSkuDescr;
        protected TextBox txtSkuID;
        protected TextBox txtToLoc;
        protected TextBox txtToLpnID;
        protected TextBox txtUomID;
        protected TextBox txtVendorID;
        protected UpdatePanel up1;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("OrderMovement.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.hdnMovementID.Value.Trim())), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                if (!string.IsNullOrEmpty(this.txtQty.Text) && (decimal.Parse(this.txtQty.Text) <= 0M))
                {
                    this.lblMsg.Text = "移动数量必须大于0";
                    this.txtQty.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtFromLoc.Text))
                {
                    this.lblMsg.Text = "请选择货位!";
                    this.txtFromLoc.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtSkuID.Text))
                {
                    this.lblMsg.Text = "请选择货品!";
                    this.txtSkuID.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtToLoc.Text))
                {
                    this.lblMsg.Text = "请选择至货位!";
                    this.txtToLoc.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtQty.Text))
                {
                    this.lblMsg.Text = "请输入移动数量!";
                    this.txtQty.Focus();
                    return;
                }
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4100_023");
                    return;
                }
                success = this.InsertMovementDetail();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "操作异常：" + ex.Message;
                return;
            }
            if (success)
            {
                base.Response.Redirect(string.Format("OrderMovement.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.hdnMovementID.Value.Trim())), false);
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                if (!string.IsNullOrEmpty(this.txtQty.Text) && (decimal.Parse(this.txtQty.Text) <= 0M))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4100_022");
                    this.txtQty.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtFromLoc.Text))
                {
                    this.lblMsg.Text = "请选择货位!";
                    this.txtFromLoc.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtSkuID.Text))
                {
                    this.lblMsg.Text = "请选择货品!";
                    this.txtSkuID.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtToLoc.Text))
                {
                    this.lblMsg.Text = "请选择至货位!";
                    this.txtToLoc.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtQty.Text))
                {
                    this.lblMsg.Text = "请输入移动数量!";
                    this.txtQty.Focus();
                    return;
                }
                success = this.InsertMovementDetail();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_024") + ex.Message;
                return;
            }
            if (success)
            {
                this.txtLineID.Text = (int.Parse(this.txtLineID.Text) + 10).ToString();
                this.txtLot.Text = "";
                this.ddlStatus.SelectedValue = "New";
                this.txtSkuID.Text = "";
                this.txtFromLoc.Text = "";
                this.txtFromLpnID.Text = "";
                this.txtToLoc.Text = "";
                this.txtToLpnID.Text = "";
                this.txtFromQty.Text = "";
                this.txtQty.Text = "";
                this.txtRemark.Text = "";
                this.txtFromLpnID.Text = "";
                this.txtSkuDescr.Text = "";
                this.txtLot.Text = "";
                this.txtFromQty.Text = "";
                this.txtAvailableQty.Text = "";
                this.txtLotAttr01.Text = "";
                this.txtLotAttr02.Text = "";
                this.txtLotAttr03.Text = "";
                this.txtLotAttr04.Text = "";
                this.txtLotAttr05.Text = "";
                this.txtLotAttr06.Text = "";
                this.txtLotAttr07.Text = "";
                this.txtLotAttr08.Text = "";
                this.txtLotAttr09.Text = "";
                this.txtLotAttr10.Text = "";
                this.txtVendorID.Text = "";
                this.txtProduceDate.Text = "";
                this.txtReceiptDate.Text = "";
                this.txtExpiryDate.Text = "";
                this.txtExternalLot.Text = "";
            }
        }

        private bool InsertMovementDetail()
        {
            bool success = false;
            MovementDetailInfo movementDetailInfo = null;
            if (base.DataEntityViewState != null)
            {
                movementDetailInfo = base.DataEntityViewState as MovementDetailInfo;
            }
            else
            {
                movementDetailInfo = new MovementDetailInfo();
            }
            movementDetailInfo.MovementID = this.hdnMovementID.Value;
            movementDetailInfo.LineID = int.Parse(this.txtLineID.Text);
            movementDetailInfo.Lot = this.txtLot.Text;
            movementDetailInfo.Status = this.ddlStatus.SelectedValue;
            movementDetailInfo.OwnerID = this.txtOwnerID.Text;
            movementDetailInfo.SkuID = this.txtSkuID.Text;
            movementDetailInfo.FromLoc = this.txtFromLoc.Text;
            movementDetailInfo.FromLpnID = this.txtFromLpnID.Text;
            movementDetailInfo.ToLoc = this.txtToLoc.Text;
            movementDetailInfo.ToLpnID = this.txtToLpnID.Text;
            movementDetailInfo.ReasonCode = "";
            movementDetailInfo.UomID = this.txtUomID.Text;
            movementDetailInfo.PackID = this.txtPackID.Text;
            movementDetailInfo.FromQty = decimal.Parse(this.txtFromQty.Text);
            movementDetailInfo.Qty = decimal.Parse(this.txtQty.Text);
            movementDetailInfo.Remark = this.txtRemark.Text;
            if (!string.IsNullOrEmpty(this.hdnType.Value))
            {
                movementDetailInfo.UpdatedBy = movementDetailInfo.CreatedBy;
                if (!string.IsNullOrEmpty(InvMovementServiceClientProxy.GetInstance().UpdateMovementDetail(movementDetailInfo)))
                {
                    success = true;
                }
                return success;
            }
            movementDetailInfo.CreatedBy = base.UserInfo.UserID;
            movementDetailInfo.UpdatedBy = movementDetailInfo.CreatedBy;
            if (!string.IsNullOrEmpty(InvMovementServiceClientProxy.GetInstance().InsertMovementDetail(movementDetailInfo)))
            {
                success = true;
            }
            return success;
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "MovementStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                string ReceiptID = base.Request.QueryString["ID"];
                this.hdnMovementID.Value = ReceiptID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                string from = base.Request.QueryString["from"];
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["LineID"]))
            {
                string LineID = base.Request.QueryString["LineID"];
                this.txtLineID.Text = LineID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["Owner"]))
            {
                string ownerID = base.Request.QueryString["Owner"];
                this.txtOwnerID.Text = ownerID;
                this.hdnOwnerID.Value = ownerID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["type"]))
            {
                this.btnSaveContinue.Enabled = false;
                this.hdnType.Value = base.Request.QueryString["type"];
                this.SetTextBoxByReceiptDetailInfo();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTextBoxByReceiptDetailInfo()
        {
            try
            {
                MovementDetailInfo movementDetailInfo = InvMovementServiceClientProxy.GetInstance().GetAllMovementDetailByID(this.hdnMovementID.Value, this.txtLineID.Text);
                if (movementDetailInfo != null)
                {
                    this.txtLot.Text = movementDetailInfo.Lot;
                    this.ddlStatus.SelectedValue = movementDetailInfo.Status;
                    this.txtOwnerID.Text = movementDetailInfo.OwnerID;
                    this.txtSkuID.Text = movementDetailInfo.SkuID;
                    this.txtFromLoc.Text = movementDetailInfo.FromLoc;
                    this.txtFromLpnID.Text = movementDetailInfo.FromLpnID;
                    this.txtToLoc.Text = movementDetailInfo.ToLoc;
                    this.txtToLpnID.Text = movementDetailInfo.ToLpnID;
                    this.txtFromQty.Text = base.GetPreferenceDigit(movementDetailInfo.FromQty);
                    this.txtQty.Text = base.GetPreferenceDigit(movementDetailInfo.Qty);
                    this.txtRemark.Text = movementDetailInfo.Remark;
                    this.txtFromLpnID.Text = movementDetailInfo.FromLpnID;
                    this.txtOwnerID.Text = movementDetailInfo.OwnerID;
                    this.txtSkuDescr.Text = movementDetailInfo.SkuDescr;
                    this.txtLot.Text = movementDetailInfo.Lot;
                    this.txtFromQty.Text = base.GetPreferenceDigit(movementDetailInfo.FromQty);
                    this.txtAvailableQty.Text = base.GetPreferenceDigit(movementDetailInfo.AvailableQty);
                    this.txtPackID.Text = movementDetailInfo.PackID;
                    this.txtUomID.Text = movementDetailInfo.UomID;
                    this.txtLotAttr01.Text = movementDetailInfo.LotAttr01;
                    this.txtLotAttr02.Text = movementDetailInfo.LotAttr02;
                    this.txtLotAttr03.Text = movementDetailInfo.LotAttr03;
                    this.txtLotAttr04.Text = movementDetailInfo.LotAttr04;
                    this.txtLotAttr05.Text = movementDetailInfo.LotAttr05;
                    this.txtLotAttr06.Text = movementDetailInfo.LotAttr06;
                    this.txtLotAttr07.Text = movementDetailInfo.LotAttr07;
                    this.txtLotAttr08.Text = movementDetailInfo.LotAttr08;
                    this.txtLotAttr09.Text = movementDetailInfo.LotAttr09;
                    this.txtLotAttr10.Text = movementDetailInfo.LotAttr10;
                    this.txtVendorID.Text = movementDetailInfo.VendorID;
                    this.txtProduceDate.Text = base.GetPreferenceShortDate(movementDetailInfo.ProduceDate);
                    this.txtReceiptDate.Text = base.GetPreferenceShortDate(movementDetailInfo.ReceiptDate);
                    this.txtExpiryDate.Text = base.GetPreferenceShortDate(movementDetailInfo.ExpiryDate);
                    this.txtExternalLot.Text = movementDetailInfo.ExternalLot;
                    base.DataEntityViewState = movementDetailInfo;
                }
                else
                {
                    this.lblMsg.Text = this.hdnMovementID.Value + base.GetGlobalMsgResource("F4100_021");
                    this.lblMsg.Visible = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void txtSkuID_OnTextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtSkuID.Text))
            {
                InvLotLocLpnQueryEntity invlotloclpnQueryEntity = new InvLotLocLpnQueryEntity {
                    SkuID = "= '" + this.hdnSkuID.Value + "'",
                    Loc = "='" + this.hdnFromLoc.Value + "'",
                    Lot = "='" + this.hdnLot.Value + "'",
                    LpnID = "='" + this.hdnFromLpn.Value + "'",
                    CurrentPage = "0",
                    PageSize = "1"
                };
                DataSet ds = InventoryMgtServiceClientProxy.GetInstance().GetInvLotLocLpnByQueryEntity(invlotloclpnQueryEntity);
                if (((ds != null) && (ds.Tables.Count > 0)) && (ds.Tables[2].Rows.Count > 0))
                {
                    this.txtFromLoc.Text = ds.Tables[2].Rows[0]["LOC"].ToString();
                    this.txtFromLpnID.Text = ds.Tables[2].Rows[0]["LPN_ID"].ToString();
                    this.txtOwnerID.Text = ds.Tables[2].Rows[0]["OWNER_ID"].ToString();
                    this.txtSkuDescr.Text = ds.Tables[2].Rows[0]["DESCR"].ToString();
                    this.txtLot.Text = ds.Tables[2].Rows[0]["LOT"].ToString();
                    this.txtFromQty.Text = base.GetPreferenceDigit(ds.Tables[2].Rows[0]["QTY"].ToString());
                    this.txtAvailableQty.Text = base.GetPreferenceDigit(ds.Tables[2].Rows[0]["AVAILABLE_QTY"].ToString());
                    this.txtLotAttr01.Text = ds.Tables[2].Rows[0]["LOT_ATTR01"].ToString();
                    this.txtLotAttr02.Text = ds.Tables[2].Rows[0]["LOT_ATTR02"].ToString();
                    this.txtLotAttr03.Text = ds.Tables[2].Rows[0]["LOT_ATTR03"].ToString();
                    this.txtLotAttr04.Text = ds.Tables[2].Rows[0]["LOT_ATTR04"].ToString();
                    this.txtLotAttr05.Text = ds.Tables[2].Rows[0]["LOT_ATTR05"].ToString();
                    this.txtLotAttr06.Text = ds.Tables[2].Rows[0]["LOT_ATTR06"].ToString();
                    this.txtLotAttr07.Text = ds.Tables[2].Rows[0]["LOT_ATTR07"].ToString();
                    this.txtLotAttr08.Text = ds.Tables[2].Rows[0]["LOT_ATTR08"].ToString();
                    this.txtLotAttr09.Text = ds.Tables[2].Rows[0]["SKU_PROPERTY"].ToString();
                    this.txtLotAttr10.Text = ds.Tables[2].Rows[0]["LOT_ATTR10"].ToString();
                    this.txtVendorID.Text = ds.Tables[2].Rows[0]["VENDOR_ID"].ToString();
                    this.txtProduceDate.Text = base.GetPreferenceShortDate(ds.Tables[2].Rows[0]["PRODUCE_DATE"].ToString());
                    this.txtReceiptDate.Text = base.GetPreferenceShortDate(ds.Tables[2].Rows[0]["RECEIPT_DATE"].ToString());
                    this.txtExpiryDate.Text = base.GetPreferenceShortDate(ds.Tables[2].Rows[0]["EXPIRY_DATE"].ToString());
                    this.txtExternalLot.Text = ds.Tables[2].Rows[0]["EXTERNAL_LOT"].ToString();
                    this.txtPackID.Text = ds.Tables[2].Rows[0]["PACK_ID"].ToString();
                    this.txtUomID.Text = ds.Tables[2].Rows[0]["UOM_ID"].ToString();
                }
            }
        }
    }
}

